package numberslider;

import dbutils.DbUtils;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Dec 16, 2009
 * Time: 10:16:43 PM
 * To change this template use File | Settings | File Templates.
 */
public class NumberSliderForm extends JFrame {
    private String analysName = "defalut";
    private static Map<String, List<String>> analysTypes;
    private JTable jTable1;
    private ResultSet rs;
    private Statement stmt;
    private ResultSetTableModel model;
    private Connection con;


    public NumberSliderForm() throws Exception {
        initComponents();
    }

    public void initComponents() throws Exception {
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();

        JScrollPane jScrollPane1 = new JScrollPane();
        JScrollPane jScrollPane2 = new JScrollPane();

        AnalysTableModel am = new AnalysTableModel(analysName);
        JTree jTree1 = getAnalysTree();
        jTable1 = new JTable();
        jTable1.setModel(am);


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new Color(204, 204, 204));
        jPanel1.setBorder(BorderFactory.createTitledBorder("Analys Tree"));

        jTree1.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent evt) {
                try {

//                jTable1.setModel(new AnalysisTableModel(evt.getNewLeadSelectionPath().getLastPathComponent().toString()));
//                jTable1.updateUI();
                    Class.forName("com.mysql.jdbc.Driver");
                    // force loading of driver
                    String url = "jdbc:mysql://localhost/" + DbUtils.GENERAL_DATABASE_NAME;
                    String user = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, user,
                            password);
                    stmt = con.createStatement();
                    String tableName = evt.getNewLeadSelectionPath().getLastPathComponent().toString();
                    String query = "SELECT * FROM " + tableName;
                    rs = stmt.executeQuery(query);
                    model = new CachingResultSetTableModel(rs);
                    jTable1.setModel(model);
                    jTable1.updateUI();
                }
                catch (Exception e) {

                }

            }
        });

        jScrollPane1.setViewportView(jTree1);

        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);

        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 201, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(39, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 423, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(86, Short.MAX_VALUE))
        );


        jPanel2.setBackground(new Color(204, 204, 204));
        jPanel2.setBorder(BorderFactory.createTitledBorder("Analys view"));
//
        jScrollPane2.setViewportView(jTable1);
        GroupLayout jPanel2Layout = new GroupLayout(jPanel2);

        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 895, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 377, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(132, Short.MAX_VALUE))
        );

        //main layout set two panels
        GroupLayout layout = new GroupLayout(getContentPane());

        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(57, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jPanel2, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }


    private JDialog abouBox;


    private void treeValueChanged(TreeSelectionEvent evt) {
    }

    private JTree getAnalysTree() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("ANALYSIS");
        for (Map.Entry<String, List<String>> res : analysTypes.entrySet()) {
            for (String result : res.getValue()) {
                DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(result);
                child1.add(child2);
            }
        }

        root.add(child1);
        return new JTree(root);
    }

    static {
        try {
            Map<String, List<String>> map = new HashMap<String, List<String>>();
            Connection s = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = s.prepareStatement(" SELECT analysis_name FROM analysis_instr_invest_list ");
            ResultSet rs = ps.executeQuery();
            List<String> analys = new ArrayList<String>();
            while (rs.next()) {
                analys.add(rs.getString(1));
            }
            map.put("ANALYSIS", analys);
            analysTypes = map;
            s.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

